#!/bin/sh
#############################################################################
#
#	This script gets called in two ways,  the first is to set the
#	default config up.  The second "final" pass is to save any settings
#	back to the vendors directory for permanent inclusion.
#
#	Copyright (C) 2001       Lineo    <davidm@snapgear.com>
#	Copyright (C) 2001-2002  SnapGear <davidm@snapgear.com>
#
#############################################################################
#
# Some basic starting points
#

CONFIG=.config
if [ -f ./.oldconfig ]
then
	. ./.oldconfig
fi

PASS="$1"
VENDOR=""
PRODUCT=""
LINUXDIR=""
LIBCDIR=""
LIBCBASEDIR=""
ROOTDIR=`pwd`
#############################################################################
#
# this code closely matches that in mkconfig,  it has to !
#

get_kernel()
{
	KERNEL="`grep '^CONFIG_DEFAULTS_KERNEL_.*=y' ${CONFIG}`"
	if [ -z "${KERNEL}" ]; then
		return 1
	fi
	KERNEL=${KERNEL##CONFIG_DEFAULTS_KERNEL_}
	KERNEL=${KERNEL%%=y}
	KERNEL="`echo ${KERNEL} | sed -e 's/_/./g'`.x"
	return 0
}

var_isset() # because of make evironment inheritance,  we must look in the file
{
	if grep "$1=y" $CONFIG > /dev/null
	then
		return 0
	fi
	return 1
}

fix_name()
{
	echo $1 | tr '[\-\.\/\+a-z]' '[____A-Z]'
}

#
# remove line containing $1 append line $2 to file in $3
#
replace_line()
{
	TMPR="/tmp/setconfig.$$"
	if [ -f "$3" ]
	then
		cat "$3" | grep -v "$1" > "$TMPR"
	else
		touch "$TMPR"
	fi
	echo "$2" >> "$TMPR"
	cp "$TMPR" "$3"
	rm -f "$TMPR"
}

fix_conf_files()
{
	#
	# add some bits for convienence, must be done after each stage
	#

	replace_line CONFIG_VENDOR= "CONFIG_VENDOR=$VENDOR"       .config
	replace_line CONFIG_PRODUCT= "CONFIG_PRODUCT=$PRODUCT"    .config
	replace_line CONFIG_LINUXDIR= "CONFIG_LINUXDIR=$LINUXDIR" .config
#	replace_line CONFIG_LIBCDIR= "CONFIG_LIBCDIR=$LIBCDIR"    .config
#	replace_line CONFIG_LANGUAGE= "CONFIG_LANGUAGE=$LANGUAGE" .config

	replace_line CONFIG_VENDOR "#define CONFIG_VENDOR \"$VENDOR\"" \
			 config/autoconf.h
	replace_line CONFIG_PRODUCT "#define CONFIG_PRODUCT \"$PRODUCT\"" \
			 config/autoconf.h
	replace_line CONFIG_LINUXDIR "#define CONFIG_LINUXDIR \"$LINUXDIR\"" \
			 config/autoconf.h
#	replace_line CONFIG_LIBCDIR "#define CONFIG_LIBCDIR \"$LIBCDIR\"" \
#			 config/autoconf.h
#	replace_line CONFIG_LANGUAGE "#define CONFIG_LANGUAGE \"$LANGUAGE\"" \
#			 config/autoconf.h
	replace_line VENDORS_AUTOCONF_INCLUDED \
			 "#define VENDORS_AUTOCONF_INCLUDED" config/autoconf.h
	replace_line "#undef AUTOCONF_INCLUDED" "#undef AUTOCONF_INCLUDED" \
			 config/autoconf.h

	replace_line OLD_VENDOR   "OLD_VENDOR=\"$VENDOR\""   ./.oldconfig
	replace_line OLD_PRODUCT  "OLD_PRODUCT=\"$PRODUCT\"" ./.oldconfig
	replace_line OLD_LINUXDIR "OLD_LINUXDIR=\"$LINUXDIR\"" ./.oldconfig
#	replace_line OLD_LIBCDIR  "OLD_LIBCDIR=\"$LIBCDIR\"" ./.oldconfig
#	replace_line OLD_LANGUAGE  "OLD_LANGUAGE=\"$LANGUAGE\"" ./.oldconfig
}

#############################################################################

#for i in vendors/*/*/config.arch
#do
#	IFS=/
#	set -- $i
#	IFS=" 
#	"
#	VDIR="`dirname $i`"
#	if [ -f "$VDIR/config.languages" ]
#	then
#		for j in `cat "$VDIR/config.languages"`
#		do
#			if var_isset "CONFIG_DEFAULTS_`fix_name $2`_`fix_name $3`_`fix_name $j`"
#			then
#				VENDOR="$2"
#				PRODUCT="$3"
#				#LANGUAGE="$j"
#				break 2
#			fi
#		done
#	fi
#	if var_isset "CONFIG_DEFAULTS_`fix_name $2`_`fix_name $3`"
#	then
#		VENDOR="$2"
#		PRODUCT="$3"
#		break
#	fi
#done

#############################################################################

#if [ "$VENDOR$PRODUCT" = "" ]; then
#	echo "You have not selected a Vendor/Product in the config." >&2
#	exit 1
#fi

#############################################################################
#
# kernel version
#

# get_kernel defines KERNEL for us
if get_kernel; then
	LINUXDIR="linux-${KERNEL}"
else
	echo "Unknown kernel configuration." >&2
	exit 1
fi

if [ ! -d ${LINUXDIR} ]; then
	echo "Missing kernel directory ${LINUXDIR} !" >&2
	exit 1
fi


#############################################################################
#
# libc verions (uC-libc is a special case to prevent confusion)
#

#for i in uC-libc uClibc uClibc-* glibc glibc-*
#do
#	if var_isset "CONFIG_DEFAULTS_LIBC_`fix_name $i`"
#	then
#		LIBCNAME="`echo ${i} | sed 's/-[0-9].*$//g'`"
#		LIBCBASEDIR=${i}
#		case "$i" in
#		uC-libc) LIBCDIR="libc"; LIBCBASEDIR="$LIBCDIR" ;;
#		glibc*)  LIBCDIR="$LIBCBASEDIR/build" ;;
#		*)       LIBCDIR="$LIBCBASEDIR" ;;
#		esac
#		break
#	fi
#done

#############################################################################
#
# Now install the configs,  careful not dump on them unless they asked us to
#

if [ "$PASS" != "final" ]; then

	#
	# if the user touches any important settings then force a clean
	# otherwise bad things can happen in the tree
	#
	if [ "$OLD_LANGUAGE$OLD_PRODUCT$OLD_VENDOR$OLD_LINUXDIR$OLD_LIBCDIR" != \
			"$LANGUAGE$PRODUCT$VENDOR$LINUXDIR$LIBCDIR" ]; then
		if [ -f .oldconfig ]; then
			cp .config .config.save
			cp .oldconfig .config
			echo; echo; echo; echo
			echo "**** Cleaning tree for old settings ****"
			echo "rm -rf romfs; make clean > /dev/null 2>&1"
			echo; echo; echo; echo
			rm -rf romfs
			make clean > /dev/null 2>&1
			cp .config.save .config
			rm -f .config.save
		fi
#		rm -f $LIBCDIR/.config
		rm -f $LINUXDIR/.config
#		rm -f $ROOTDIR/user/busybox/.config
		rm -f config/.config
#		rm -f config.arch
#		[ ! -d modules ] || rm -f modules/.config
	fi

    # once before the oldconfig stuff is run
	fix_conf_files

	if [ ! -f $LINUXDIR/.config ] || var_isset CONFIG_DEFAULTS_OVERRIDE; then
		if [ ! -f "$LINUXDIR/arch/mips/configs/rtl865xc_defconfig" ]; then
			echo; echo; echo; echo
			echo "*********************** NOTICE ****************************"
			echo "There is no preset config for $LINUXDIR on this platform." 
			echo "If you choose to continue you will have to setup a linux"
			echo "config from scratch which is a very complex task."
			echo "You will find it easier if you can copy the config from"
			echo "another platform that is similar."
			echo
			echo "If you know what you are doing press <ENTER> otherwise"
			echo "Type ^C to abort."
			read dummy
		else
			make -C $LINUXDIR ARCH=mips rtl865xc_defconfig
			#cp "$LINUXDIR/arch/mips/rtl8652_defconfig" $LINUXDIR/.config
		fi
		make oldconfig_linux
	fi

	#
	# everything about modules is optional,  optional modules dir
	# and optional modules config files
	#
#	if [ -d modules ]; then
#		TMP=`expr $LINUXDIR : "[^-][^-]*\(-.*\)"`
#		if [ -f "vendors/$VENDOR/$PRODUCT/config.modules$TMP" ]
#		then
#			TMP="vendors/$VENDOR/$PRODUCT/config.modules$TMP"
#		else
#			TMP="vendors/$VENDOR/$PRODUCT/config.modules"
#		fi
#		if [ ! -f modules/.config ] || var_isset CONFIG_DEFAULTS_OVERRIDE; then
#			if [ -f "$TMP" ]; then
#				cp "$TMP" modules/.config
#			fi
#			make oldconfig_modules
#		fi
#	fi

#	if [ ! -f config/.config ] || var_isset CONFIG_DEFAULTS_OVERRIDE; then
#		TMP=`expr $LINUXDIR : "[^-][^-]*\(-.*\)"`
#		if [ -f "vendors/$VENDOR/$PRODUCT/config.vendor$TMP" ]
#		then
#			cp "vendors/$VENDOR/$PRODUCT/config.vendor$TMP" config/.config
#		elif [ -f "vendors/$VENDOR/$PRODUCT/config.vendor" ]
#		then
#			cp "vendors/$VENDOR/$PRODUCT/config.vendor" config/.config
#		else
#			echo; echo; echo; echo
#			echo "*********************** NOTICE ****************************"
#			echo "There is no preset config for applications on this platform." 
#			echo "If you choose to continue you will have to setup your"
#			echo "application config from scratch.  You will find it easier"
#			echo "if you can copy the config from another platform that is"
#			echo "similar."
#			echo
#			echo "If you press <ENTER> you will be forced into the application"
#			echo "config screen as part of the config process."
#			echo "You may type ^C to abort."
#			read dummy
			#
			# force config of user apps
			#
#			replace_line CONFIG_DEFAULTS_VENDOR= \
#					"CONFIG_DEFAULTS_VENDOR=y" .config
#		fi
#		make oldconfig_config
#	fi

#	if [ -L lib/$LIBCBASEDIR ]; then
#		rm -f lib/$LIBCBASEDIR
#	fi

#	if [ ! -e lib/$LIBCBASEDIR -a -e $LIBCBASEDIR/. ] ; then
#		ln -s "`pwd`/$LIBCBASEDIR" "lib/$LIBCBASEDIR"
#	fi

#	case "$LIBCBASEDIR" in
#	uClibc*)
#		if [ ! -f lib/$LIBCDIR/.config ] || var_isset CONFIG_DEFAULTS_OVERRIDE; then
#			if [ -f "vendors/$VENDOR/$PRODUCT/config.$LIBCBASEDIR" ]; then
#				cp "vendors/$VENDOR/$PRODUCT/config.$LIBCBASEDIR" \
#						lib/$LIBCDIR/.config
#			elif [ -f "vendors/$VENDOR/$PRODUCT/config.$LIBCNAME" ]; then
#				cp "vendors/$VENDOR/$PRODUCT/config.$LIBCNAME" \
#						lib/$LIBCDIR/.config
#			else
#				echo; echo; echo; echo
#				echo "********************** NOTICE ***************************"
#				echo "There is no preset config for $LIBCBASEDIR on this" 
#				echo "platform.  $LIBCBASEDIR cannot build without this."
#				echo "You should copy a config.$LIBCBASEDIR from another"
#				echo "target that is similar to yours to your chosen"
#				echo "vendor/../.. directory and check that it is correct."
#				echo
#				echo "Once you have done that start the config process again."
#				exit 1
#			fi
#			make oldconfig_uClibc
#		fi
#		;;
#	esac

	# and once after to clean up
	fix_conf_files
fi

#############################################################################
#
# The override config option is always turned off when we are done so that
# someone doesn't get all upset that they lost their config files.
#
# Always add the VENDOR/PRODUCT dir for the build to work
#

if [ "$PASS" = "final" ]; then

	fix_conf_files

	#
	# check for other config options
	#

	if var_isset CONFIG_DEFAULTS_OVERRIDE; then
		sed 's/^\(CONFIG_DEFAULTS_OVERRIDE\)=y/# \1 is not set/' < .config \
				> .config.tmp
		cp .config.tmp .config
		rm -f .config.tmp
	fi

#	if var_isset CONFIG_DEFAULTS_VENDOR_UPDATE; then

#		cp $LINUXDIR/.config "vendors/$VENDOR/$PRODUCT/config.$LINUXDIR"
#		TMP=`expr $LINUXDIR : "[^-][^-]*\(-.*\)"`
#		if [ -f "vendors/$VENDOR/$PRODUCT/config.vendor$TMP" ]
#		then
#			cp config/.config "vendors/$VENDOR/$PRODUCT/config.vendor$TMP"
#		else
#			cp config/.config "vendors/$VENDOR/$PRODUCT/config.vendor"
#		fi
#		if [ -f modules/.config ]; then
#			if [ -f "vendors/$VENDOR/$PRODUCT/config.modules$TMP" ]
#			then
#				cp modules/.config "vendors/$VENDOR/$PRODUCT/config.modules$TMP"
#			else
#				cp modules/.config "vendors/$VENDOR/$PRODUCT/config.modules"
#			fi
#		fi
#		if [ -f "$LIBCDIR/.config" ]; then
#			if [ "$LIBCBASEDIR" = "$LIBCNAME" ]; then
#			  cp $LIBCDIR/.config "vendors/$VENDOR/$PRODUCT/config.$LIBCNAME"
#			else
#			  cp $LIBCDIR/.config "vendors/$VENDOR/$PRODUCT/config.$LIBCBASEDIR"
#			fi
#		fi

#		sed 's/^\(CONFIG_DEFAULTS_VENDOR_UPDATE\)=y/# \1 is not set/' <.config\
#				> .config.tmp
#		cp .config.tmp .config
#		rm -f .config.tmp
#	fi

#	grep -v CONFIG_DEFAULTS_VENDOR= < .config | \
#		grep -v CONFIG_DEFAULTS_MODULES= | \
#		grep -v CONFIG_DEFAULTS_KERNEL= > .config.tmp

#	cp .config.tmp .config
#	rm -f .config.tmp
fi

#############################################################################
exit 0
